package com.example.weatherpredict.ui;


import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.weatherpredict.R;
import com.example.weatherpredict.entity.Weather;


public class WeatherDetailFragment extends Fragment {
    private Weather.DailyBean mWeatherItem;  //用来设置UI

    private TextView mDeatilDate;
    private TextView mDetailMaxTemp;
    private TextView mDetailMinTemp;
    private ImageView mDetialIcon;
    private TextView mDetailDesc;
    private TextView mDetailHumidity;
    private TextView mDetailPressure;
    private TextView mDetailWind;

    private static final String TAG = "WeatherDetailFragment";
    private static final String ARG_ITEM = "args_item";
    private String unit_text = "°";

    public static WeatherDetailFragment newInstance(Weather.DailyBean weatherItem) {
        Bundle args = new Bundle();
        args.putSerializable(ARG_ITEM, weatherItem);
        WeatherDetailFragment fragment = new WeatherDetailFragment();
        fragment.setArguments(args);
        return fragment;
    }

    //获取drawable图标资源的id
    public int getIconId(Context mContext, String icon) {
        int i = getResources().getIdentifier(icon, "drawable", mContext.getPackageName());
        if (i > 0) {
            Log.i(TAG, "Success to get drawable resource");
        } else {
            Log.i(TAG, "Fail to get drawable resource");
        }
        return i;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
        mWeatherItem = (Weather.DailyBean) getArguments().getSerializable(ARG_ITEM);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_weather_detail, container, false);
        if (getActivity().getSharedPreferences("setting", Context.MODE_PRIVATE).getString("unit", "摄氏度") == "华氏度") {
            unit_text = "℉";
        }

        mDeatilDate = (TextView) v.findViewById(R.id.detail_date);
        mDetailMaxTemp = (TextView) v.findViewById(R.id.detail_max_temp);
        mDetailMinTemp = (TextView) v.findViewById(R.id.detail_min_temp);
        mDetialIcon = (ImageView) v.findViewById(R.id.detail_icon);
        mDetailHumidity = (TextView) v.findViewById(R.id.detail_humidity);
        mDetailPressure = (TextView) v.findViewById(R.id.detail_pressure);
        mDetailWind = (TextView) v.findViewById(R.id.detail_wind);
        mDetailDesc = (TextView) v.findViewById(R.id.detail_desc);

        mDeatilDate.setText(mWeatherItem.fxDate);
        mDetailMaxTemp.setText(mWeatherItem.tempMax + unit_text);
        mDetailMinTemp.setText(mWeatherItem.tempMin + unit_text);
        String icon = "a" + mWeatherItem.iconDay;
        int id = getIconId(getContext(), icon);
        Drawable drawable = getResources().getDrawable(id);
        mDetialIcon.setImageDrawable(drawable);
        mDetailHumidity.setText("Humidity: " + mWeatherItem.humidity + " %");
        mDetailPressure.setText("Pressure: " + mWeatherItem.pressure + " hPa");
        mDetailWind.setText("Wind: " + mWeatherItem.windSpeedDay + " km/h SE");
        mDetailDesc.setText(mWeatherItem.textDay);

        return v;
    }

    public String createMessage() {
        String message = "";
        message += "今天的天气状况为：" + mWeatherItem.textDay;
        message += "    今天的最高温度是： " + mWeatherItem.tempMax;
        message += "    今天的最低温度是： " + mWeatherItem.tempMin;
        message += "    今天的湿度为： " + mWeatherItem.humidity;
        message += "    今天的风速为：" + mWeatherItem.windSpeedDay;
        message += "    今天的气压为：" + mWeatherItem.pressure;
        message += "    希望您拥有美好的一天!";
        return message;
    }

    @Override
    public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        if (getActivity().findViewById(R.id.fragment_container) == null) {    //是手机模式
            inflater.inflate(R.menu.fragment_detail, menu);
        }
    }


    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case R.id.menu_setting:
                Intent intent = new Intent(getActivity(), SettingActivity.class);
                startActivity(intent);
                return true;
            case R.id.menu_share:
                Intent i = new Intent(Intent.ACTION_SEND);
                i.setType("text/plain");
                i.putExtra(Intent.EXTRA_TEXT, createMessage());
                startActivity(i);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }


}
